package tool;
import java.util.List;

import com.base.bean.*;
import com.base.domain.*;
import com.base.service.AddressService;
import tool.encrypt.*;

/**单例模式：
 * 用于客户详细信息生成json
 * @author Administrator
 *
 */

public class HtmlTool {
	//单例模式 start
	private volatile static HtmlTool instance = null;	
	private StringBuffer sb;
//	private static String tmp = "{\"clientBase\":\"#CB#\",\"clientExtend\":\"#CE#\",\"other\":\"#OTHER#\"}";
	
	private HtmlTool() {
		sb=new StringBuffer();
	}
	public static HtmlTool getInstance() {
		if (instance == null) {
			synchronized (HtmlTool.class) {// 1
				if (instance == null) {// 2
					instance = new HtmlTool();// 3
					
				}
			}
		}
		return instance;
	}
	//单例模式 end
	
	/* 
	 	点的转义：. ==> u002E 
		美元符号的转义：$ ==> u0024 
		乘方符号的转义：^ ==> u005E 
		左大括号的转义：{ ==> u007B 
		左方括号的转义：[ ==> u005B 
		左圆括号的转义：( ==> u0028 
		竖线的转义：| ==> u007C 
		右圆括号的转义：) ==> u0029 
		星号的转义：* ==> u002A 
		加号的转义：+ ==> u002B 
		问号的转义：? ==> u003F 
		反斜杠的转义： ==> u005C 

		[{"clientBase"："#CB#",
	  	 "clientExtend":"#CE#",
	  	  "other":"#OTHER#"},
	  	  {},{}	
	    ]
	    
	          转义后：
	    \u005B{\"clientBase\"：\"#CLIENTBASE#\",
	  	 \"clientExtend\":\"#CLIENTeXTEND#\",
	  	  \"other\":\"#OTHER#\"},
	  	  {},{}	
	    ]


	 */
	
	/**
	 * 返回List<ClientDetail> json格式的字符串,做了html适应性
	 * @return
	 */
	public StringBuffer htmlClientDetail(List<ClientDetail> clientDetailList){
//		String tmp="\u005B{\"clientBase\"：\"#CB#\", \"clientExtend\":\"#CE#\", \"other\":\"#OTHER#\"}]";
		AddressService as=AddressService.getInstance();
		sb.setLength(0);	//此种方式清空效率比较高
		sb.append("\u005B");	//左方括号
		for(int i=0;i<clientDetailList.size();i++){
			ClientBase cb=clientDetailList.get(i).getClientBase();
			String name=cb.getName();
			String city="";
			String area="";
			if(cb.getCityId()!=null)
				city=as.getNameByCode(AddressService.ADD_TYPE.CITY, cb.getCityId());
			if(cb.getAreaId()!=null)
				area=as.getNameByCode(AddressService.ADD_TYPE.AREA, cb.getAreaId());
			List<ClientPhone> cps=clientDetailList.get(i).getClientPhones();
			String phones="";
			for(int j=0;j<cps.size();j++){	
				// 一键拨号   <a href="tel:10086">马上拨打电话10086</a>
				phones=phones+"<a href='tel\\:"+cps.get(j).getPhone()+"'>"+cps.get(j).getPhone()+"</a><br>";
//				if(j<cps.size()-1)
//					phones=phones+"<br>";
			}
			ClientExtend ce=clientDetailList.get(i).getClientExtend();
			String memo="";
			if(ce.getMemo()!=null)
				memo=ce.getMemo();
			sb.append("{\"cid\":"+cb.getCid()+",");
			sb.append("\"clientBase\":\"");
			sb.append(name+"<br>");
			
			sb.append("(微信客户编码\\:");
			sb.append(IdentifyTool.enCodeCid(cb.getCid())+")<br>");
			
			sb.append(city+"\u007C"+area+"<br>");
			sb.append(phones);
			sb.append("备注\\:"+memo);	//基本信息End
			sb.append("\",\"clientExtend\":\"");	//扩展信息START
			
			String ygsb=ce.getYgsbType();	//欲购设备类型
			String wjj=ce.getYywjj();	//已有挖掘机
			String zzj=ce.getYyzzj();	//已有装载机
			String sellplan="";
			if(ce.getSellPlan()!=null)
				sellplan=ce.getSellPlan().toString().substring(0,10);
			String khType="";
			//'a':'无&nbsp;','b':'作废&chi;','c':'标记&clubs;','d':'一般&diams;','e':'重要&spades;'
			if(ce.getClientType()!=null){
				if(ce.getClientType().equals("a"))
					khType="&nbsp;";	//无
				else if(ce.getClientType().equals("b"))
					khType="&clubs;";		//标记
				else if(ce.getClientType().equals("c"))
					khType="&diams;";	//一般
				else if(ce.getClientType().equals("d"))
					khType="&hearts;";	//重要
				else if(ce.getClientType().equals("e"))
					khType="&spades;";	//非常重要
			}
			
			sb.append("客户类型\\:"+khType+"<br>");
			sb.append("已有挖掘机\\:"+wjj+"<br>");
			sb.append("已有装载机\\:"+zzj+"<br>");
			sb.append("预计达成时间\\:"+sellplan+"<br>");
			sb.append("欲购设备类型\\:"+ygsb+"<br>");
			
			sb.append("\", \"other\":\"");	//Other START
			
			sb.append("\"}");
			
			if(i<clientDetailList.size()-1)
				sb.append(",");
		}
		
		
		
		
		sb.append("]");	// end
		return sb;
	}
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
//		System.out.println(tmp);
	}

}
